#include "keyvalue.hpp"

using namespace configraion;
using namespace storage;

bool keyvalue::ReocrdInit(keyvalue *tKeyValueDemo, uint16_t usMaxRecord)
{
	file::RecordInitTPDF tRecordInit;
	uint32_t i;
	uint16_t j;
	
	tRecordInit.usItemLength = usMaxRecord;
	tRecordInit.usRecordID = tKeyValueDemo->mRecordId;
	j = 0;
	for(i = 0; i < tKeyValueDemo->KeyLen; i++)
	{
		j += tKeyValueDemo->KeyDemo[i].usLen + 6;
	}
	tRecordInit.usItemSize = j;
	return(file::RecordInit(tKeyValueDemo->mDisk, &tRecordInit));
}
///>记录键值
keyvalue::keyvalue(file::DiskTPDF tDisk, uint16_t usReocrdId)
{
	KeyDemo = 0;
    KeyLen = 0;
	mDisk = tDisk;
	mRecordId = usReocrdId;
	mIsRecord = true;
}
///变量键值
keyvalue::keyvalue(file::DiskTPDF tDisk, const char *cFileName)
{
	KeyDemo = 0;
    KeyLen = 0;
    mDisk = tDisk;
    mFileName = cFileName;
    mIsRecord = false;
}
///> 键值保存
bool keyvalue::Save(void *tag)
{
	uint8_t *mem;
	uint32_t memlen = 0;
	uint32_t i, j;

	if((!KeyDemo) || (!KeyLen) || (!tag))
	{
		return(false);
	}
	for(i = 0; i < KeyLen; i++)
	{
		memlen += KeyDemo[i].usLen + 6;
	}
	mem = (uint8_t *)malloc(memlen);
	j = 0;
    ///> 变量转换为模板值转换为模板值
	for(i = 0; i < KeyLen; i++)
	{
		memcpy(&mem[j], &KeyDemo[i].uiKey, 4);
		memcpy(&mem[j + 4], &KeyDemo[i].usLen, 2);
		memcpy(&mem[j + 6], ((uint8_t *)tag) + KeyDemo[i].uiOffset, KeyDemo[i].usLen);
		j += KeyDemo[i].usLen + 6;
	}
	if(mIsRecord)
	{   ///> 记录保存
		if(!file::Write(mDisk, mRecordId, mem, memlen))
		{
			free(mem);
			return(false);
		}
	}
	else
	{   ///> 变量保存
		if(!file::Write(mDisk, mFileName, mem, memlen))
		{
			free(mem);
			return(false);
		}
	}
	free(mem);
	return(true);
}
///> 键值打开
bool keyvalue::Open(void *tag)
{
	uint8_t *mem;
	uint32_t memlen = 0;
	uint32_t i, j;
	uint32_t keyhandle, keylen;
	
	if((!KeyDemo) || (!KeyLen) || (!tag) || (mIsRecord))
	{
		return(false);
	}
	for(i = 0; i < KeyLen; i++)
	{
		memlen += KeyDemo[i].usLen + 6;
	}
	mem = (uint8_t *)malloc(memlen);
	if(!file::Read(mDisk, mFileName, mem, memlen))///> 根据变量名找到变量实际内容
	{
		free(mem);
		return(false);
	}
    ///>变量模板转换为实际变量值
	for(i = 0; i < memlen;)
	{
		keyhandle = mem[i] + (mem[i + 1] << 8) + (mem[i + 2] << 16) + (mem[i + 3] << 24);
		keylen = mem[i + 4] + (mem[i + 5] << 8);

		for(j = 0; j < KeyLen; j++)
		{
			if(KeyDemo[j].uiKey == keyhandle)
			{
				if(KeyDemo[j].usLen < keylen)
				{
					memcpy(((uint8_t *)tag) + KeyDemo[j].uiOffset, &mem[i + 6], KeyDemo[j].usLen);
				}
				else
				{
					memcpy(((uint8_t *)tag) + KeyDemo[j].uiOffset, &mem[i + 6], keylen);
				}
				break;
			}
		}
		i += 6 + keylen;
	}
	free(mem);
	return(true);
}

bool keyvalue::Open(void *tag, uint16_t usIndex)
{
	uint8_t *mem;
	uint32_t memlen = 0;
	uint32_t i, j;
	uint32_t keyhandle, keylen;
	
	if((!KeyDemo) || (!KeyLen) || (!tag) || (!mIsRecord))
	{
		return(false);
	}
	for(i = 0; i < KeyLen; i++)
	{
		memlen += KeyDemo[i].usLen + 6;
	}
	mem = (uint8_t *)malloc(memlen);
	if(!file::Read(mDisk, mRecordId, usIndex, mem, memlen))
	{
		free(mem);
		return(false);
	}
	for(i = 0; i < memlen;)
	{
		keyhandle = mem[i] + (mem[i + 1] << 8) + (mem[i + 2] << 16) + (mem[i + 3] << 24);
		keylen = mem[i + 4] + (mem[i + 5] << 8);

		for(j = 0; j < KeyLen; j++)
		{
			if(KeyDemo[j].uiKey == keyhandle)
			{
				if(KeyDemo[j].usLen < keylen)
				{
					memcpy(((uint8_t *)tag) + KeyDemo[j].uiOffset, &mem[i + 6], KeyDemo[j].usLen);
				}
				else
				{
					memcpy(((uint8_t *)tag) + KeyDemo[j].uiOffset, &mem[i + 6], keylen);
				}
				break;
			}
		}
		i += 6 + keylen;
	}
	free(mem);
	return(true);
}

bool keyvalue::Save(void)
{
    return(false);
}

bool keyvalue::Open(void)
{
    return(false);
}
